package com.xykj.enterprise.wechat.busi.service.identity;

import com.xykj.enterprise.wechat.bean.ext.identity.JsapiTicketRsp;
import com.xykj.enterprise.wechat.busi.config.ExtClient;
import com.xykj.enterprise.wechat.busi.service.corp.CorpService;
import com.xykj.enterprise.wechat.core.model.dodb.AuthCorp;
import com.xykj.enterprise.wechat.core.model.dodb.JsapiTicket;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author george
 * @create 2021-04-21 15:16
 */
@Slf4j
public class JsapiTicketUtil {

    /**
     * 获取 应用 jsapi_ticket
     * @param jsapiTicketService
     * @param corpid
     * @param agentId
     * @param suiteId
     * @param secret
     * @param corpService
     * @param extClient
     * @return
     */
    public static String getAgentTicket(
            JsapiTicketService jsapiTicketService,
            String corpid,
            String agentId,
            String suiteId,
            String secret,
            CorpService corpService,
            ExtClient extClient
    ){
        JsapiTicket ticket = jsapiTicketService.getAgentTicket(corpid, agentId);
        String jsapi_ticket;
        if (ticket == null) {
            // 重新获取 应用 jsapi_ticket
            AuthCorp authCorp = corpService.getByCorpid(
                    corpid,
                    suiteId,
                    secret
            );

            Map<String, Object> param = new HashMap<>();
            param.put("access_token", authCorp.getAccessToken());
            JsapiTicketRsp rsp = extClient.get("GetAgentJsapiTicket", param, JsapiTicketRsp.class);
            jsapiTicketService.saveAgentTicket(corpid, agentId, rsp.getTicket(), rsp.getExpires_in());

            jsapi_ticket = rsp.getTicket();
        } else {
            jsapi_ticket = ticket.getAgentJsapiTicket();
        }
        return jsapi_ticket;
    }

    /**
     * 获取 应用 jsapi_ticket
     * @param jsapiTicketService
     * @param corpid
     * @param agentId
     * @param suiteId
     * @param secret
     * @param corpService
     * @param extClient
     * @return
     */
    public static String getCorpTicket(
            JsapiTicketService jsapiTicketService,
            String corpid,
            String agentId,
            String suiteId,
            String secret,
            CorpService corpService,
            ExtClient extClient
    ){
        JsapiTicket ticket = jsapiTicketService.getCorpTicket(corpid, agentId);
        String jsapi_ticket;
        if (ticket == null) {
            // 重新获取 应用 jsapi_ticket
            AuthCorp authCorp = corpService.getByCorpid(
                    corpid,
                    suiteId,
                    secret
            );

            Map<String, Object> param = new HashMap<>();
            param.put("access_token", authCorp.getAccessToken());
            JsapiTicketRsp rsp = extClient.get("GetCorpJsapiTicket", param, JsapiTicketRsp.class);
            log.debug("企业 jsapi ticket :",rsp);
            jsapiTicketService.saveCorpTicket(corpid, agentId, rsp.getTicket(), rsp.getExpires_in());

            jsapi_ticket = rsp.getTicket();
        } else {
            jsapi_ticket = ticket.getCorpJsapiTicket();
        }
        return jsapi_ticket;
    }

}
